package com.songqintuan.mall.customer;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.util.DESPlusUtil;
import com.songqintuan.common.util.DateTimeUtil;
import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.common.util.ValidatorUtil;
import com.songqintuan.mall.constant.MallConstants;
import com.songqintuan.mall.constant.MallManageBeanConstants;
import com.songqintuan.mall.constant.MallSpringBeanConstants;
import com.songqintuan.model.po.Customer;
import com.songqintuan.model.po.CustomerLog;
import com.songqintuan.model.vo.CustomerVo;
import com.songqintuan.persist.service.hibernate.ICustomerLogService;
import com.songqintuan.persist.service.hibernate.ICustomerService;

@ManagedBean(name="loginCustomerPage")
@ViewScoped
public class LoginCustomerPageBean implements java.io.Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CustomerVo customerVo = new CustomerVo();
	private List<String> errorMessages = new ArrayList<String>();
	public CustomerVo getCustomerVo() {
		return customerVo;
	}

	public void setCustomerVo(CustomerVo customerVo) {
		this.customerVo = customerVo;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public LoginCustomerPageBean(){
	}

	public String loginCustomer() {
		this.getErrorMessages().clear();
		validatorLoginName(this.getErrorMessages(),this.getCustomerVo());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		validatorLoginPassword(this.getErrorMessages(),this.getCustomerVo());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		ICustomerService<Customer,String> customerService = (ICustomerService<Customer,String>) SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(String.class);
		Customer newCustomer = new Customer();
		BeanCopier copyHere = BeanCopier.create(CustomerVo.class,Customer.class,false);
		copyHere.copy(this.getCustomerVo(), newCustomer, null);
		Customer customer = customerService.findUniqueBy("loginName", this.getCustomerVo().getLoginName());
		Timestamp loginTime = DateTimeUtil.getCurrentTimestamp();
		if (null == customer) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"loginname.notexist.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomerVo().getLoginName()).getDetail());
			return null;
		}else {
			if(null == customer.getStatus() || "".equals(customer.getStatus())) {
				this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"customer.status.empty.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomerVo().getLoginName()).getDetail());
				return null;
			} else {
				if (!(customer.getStatus().equals(MallConstants.CUSTOMER_STATUS_ACTIVE))) {
					this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"customer.inactiveorlocked.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomerVo().getLoginName()).getDetail());
					return null;
				}
			}
			String password = (String) this.getCustomerVo().getLoginPassword();
			password = DESPlusUtil.encrypt(password);
			String realyPassword = (String) customer.getLoginPassword();
			if (!(password.equals(realyPassword))) {
				this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"password.input.wrong.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
				return null;
			}
			
			//customer info set into session
			CurrentCustomerInfoBean currentCustomer = (CurrentCustomerInfoBean)SpringFacesUtil.getManagedBean(MallManageBeanConstants.CURRENT_CUSTOMER_BEAN_NAME);
			Customer oriCustomer = (null == currentCustomer.getCustomer())?null:currentCustomer.getCustomer();
			currentCustomer.setCustomer(customer);
			currentCustomer.copyCustomerToCustomerVo();
			ICustomerLogService<CustomerLog,String> customerLogService = (ICustomerLogService<CustomerLog,String>)SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_LOG_SERVICE_BEAN_NAME);
			customerLogService.setEntityClass(CustomerLog.class);
			customerLogService.setPKClass(String.class);
			//写customerLog信息
			if (null != currentCustomer.getCustomerLog()) {
				CustomerLog tempCustomerLog = currentCustomer.getCustomerLog();
				if (null == tempCustomerLog.getCustomer() || "".equals(tempCustomerLog.getCustomer().getLoginName())) {
					tempCustomerLog.setCustomer(customer);
					tempCustomerLog.setIp(SpringFacesUtil.getIpAddress());
					tempCustomerLog.setLoginTime(loginTime);
					tempCustomerLog.setType(MallConstants.LOGIN_IN_TYPE);
					customerLogService.saveOrUpdate(tempCustomerLog);
				} else {
					if (!(oriCustomer.getLoginName().equals(customer.getLoginName()))) {
						CustomerLog newLoginLog = new CustomerLog();
						newLoginLog.setCustomer(customer);
						newLoginLog.setIp(SpringFacesUtil.getIpAddress());
						newLoginLog.setType(MallConstants.LOGIN_IN_TYPE);
						newLoginLog.setLoginTime(loginTime);
						customerLogService.save(newLoginLog);
						currentCustomer.setCustomerLog(newLoginLog);
					}
				}
				
			} else { 
				//全新log信息
				CustomerLog newLoginLog = new CustomerLog();
				newLoginLog.setCustomer(customer);
				newLoginLog.setIp(SpringFacesUtil.getIpAddress());
				newLoginLog.setType(MallConstants.LOGIN_IN_TYPE);
				newLoginLog.setLoginTime(loginTime);
				customerLogService.save(newLoginLog);
				currentCustomer.setCustomerLog(newLoginLog);
			}
			////写入LOGINLOG信息 结束
		}
		return "/index";
	}
	public String loginOffCustomer() {
		this.getErrorMessages().clear();
		this.setCustomerVo(new CustomerVo());
		//
		CurrentCustomerInfoBean currentCustomer = (CurrentCustomerInfoBean)SpringFacesUtil.getManagedBean(MallManageBeanConstants.CURRENT_CUSTOMER_BEAN_NAME);
		currentCustomer.setCustomer(new Customer());
		currentCustomer.setCustomerLog(new CustomerLog());
		return MallConstants.GO_TO_LOGIN;
	}
	
	private void validatorLoginName(List<String> errorMessages, CustomerVo customer) {
		String loginNameLabelN18 = MessageFactory.getMessage(
				MallConstants.USER_BUNDLE_NAME,
				"customer.loginname.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		ValidatorUtil.EmptyValidatorForInput(errorMessages,
				customer.getLoginName(), loginNameLabelN18);
	}
	private void validatorLoginPassword(List<String> errorMessages, CustomerVo customer) {
		String loginPasswordLabelN18 = MessageFactory.getMessage(
				MallConstants.USER_BUNDLE_NAME,
				"customer.loginpassword.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		ValidatorUtil.EmptyValidatorForInput(errorMessages,
				customer.getLoginPassword(), loginPasswordLabelN18);
	}
}
